package com.yanqu.road.server.logic.thread.translate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ChannelInfo;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.sdk.TranslateLanguageMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @Author wpj
 * @Date 2021/4/15
 */
public class ExTranslateLanguageTask extends GeneralTask {

    GamePlayer player;
    String content;
    String language;
    long msgIndex;

    private static final Logger log = LogManager.getLogger(ExTranslateLanguageTask.class);

    public ExTranslateLanguageTask(GamePlayer player, String content, String language, long msgIndex) {
        this.player = player;
        this.content = content;
        this.language = TranslateLanguageMgr.languageCodeTransfer(language);
        this.msgIndex = msgIndex;
    }

    @Override
    public void doRun() {
        try {
            if (ServerListMgr.isOpenTranslateChat()) {
                ChannelInfo channelConfig = ServerListMgr.getChannelInfoMap().get(ServerListMgr.getServerInfo(ConfigHelper.getLong("serverId")).getChannelId());
                String[] translationsParamArr = channelConfig.getTranslateUrl().split("\\|");
                String url = translationsParamArr[0];
                String key = translationsParamArr[1];
                String secretId = translationsParamArr[2];
                String afterContent = TranslateLanguageMgr.getContent(this.content, language);
                if (afterContent == null) {
                    SortedMap<String, String> paramMap = new TreeMap<>();
                    paramMap.put("text", content);
                    paramMap.put("target", language);
                    paramMap.put("secret_id", secretId);
                    paramMap.put("timestamp", String.valueOf(DateHelper.getCurrentSecond()));

                    String sign = SignHelper.hmacSha256(paramMap, key);
                    paramMap.put("sign", sign);

                    String result = HttpHelper.doPostJson(url, JSONObject.toJSONString(paramMap), "UTF-8");
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    int ret = jsonObject.getIntValue("result");
                    if (ret == 1) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        afterContent = data.getString("text");
                        TranslateLanguageMgr.addTranslateContent(content, language, afterContent);
                    } else {
                        String errMsg = jsonObject.getString("msg");
                        int code = jsonObject.getIntValue("code");
                        log.error("translate language error, code:{}, msg:{}", code, errMsg);
                        afterContent = content;
                        return;
                    }
                }
                WorldMessageProto.TranslationRespMsg.Builder msg = WorldMessageProto.TranslationRespMsg.newBuilder();
                msg.setRet(0);
                msg.setAfterTranslateContent(afterContent);
                msg.setMsgIndex(msgIndex);
                player.sendPacket(ClientProtocol.U_TRANSLATE_LANGUAGE, msg);
            }
        } catch (Exception ex) {
            log.error(ex);
        }
    }
}
